package com.atguigu.orderservice.service.impl;

import com.atguigu.commonutils.OrderNoUtil;
import com.atguigu.commonutils.R;
import com.atguigu.commonutils.ordervo.CourseWebVoOrder;
import com.atguigu.commonutils.ordervo.UcenterMemberOrder;
import com.atguigu.orderservice.client.EduClient;
import com.atguigu.orderservice.client.UcenterClient;
import com.atguigu.orderservice.entity.Order;
import com.atguigu.orderservice.mapper.OrderMapper;
import com.atguigu.orderservice.service.OrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author fxy
 * @since 2022-05-15
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private EduClient eduClient;

    @Autowired
    private UcenterClient ucenterClient;


    //生成订单的方法
    @Override
    public String createdOrders(String courseId, String memberId) {
        //通过远程调用获取用户信息
        UcenterMemberOrder userInfoOrder = ucenterClient.getUserInfoOrder(memberId);

        //通过远程调用获取课程信息courseId
        CourseWebVoOrder courseInfoOrder = eduClient.getCourseInfoOrder(courseId);


        //创建order对象 向order对象里面设置需要的数据
        Order order = new Order();
        order.setOrderNo(OrderNoUtil.getOrderNo());
        order.setCourseId(courseId);
        order.setCourseTitle(courseInfoOrder.getTitle());
        order.setCourseCover(courseInfoOrder.getCover());
        order.setTeacherName(courseInfoOrder.getTeacherName());
        order.setTotalFee(courseInfoOrder.getPrice());
        order.setMemberId(memberId);
        order.setMobile(userInfoOrder.getMobile());
        order.setNickname(userInfoOrder.getNickname());
        order.setStatus(0); //支付状态 订单状态（0：未支付 1：已支付）
        order.setPayType(1); //支付类型（1：微信 2：支付宝）
        baseMapper.insert(order);

        return order.getOrderNo();
    }

    @Override
    public List<Order> getCourseIndex(String memberId) {
        QueryWrapper<Order> wrapper=new QueryWrapper<>();
        wrapper.eq("member_id",memberId);
        List<Order> orders = baseMapper.selectList(wrapper);
        return orders;
    }


}
